package server;

import java.io.IOException;
import java.io.InputStream;

public class Request {
    private String method; // 请求方式，比如GET/POST
    private String url;  // 例如 /,/index.html
    private String content;
    private String removeAddr; // 请求主机
    private Integer removePort; // 请求端口号
    private InputStream inputStream;

    public Request(){

    }
    public  Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取第一行请求头信息
        String[] requestStrs = inputStr.split("\\n");
        String firstLineStr = requestStrs[0];  // GET / HTTP/1.1

        String[] strings = firstLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];
        String[] hostStr = requestStrs[1].split(":");
        this.removeAddr = hostStr[1];
        this.removePort = Integer.parseInt(hostStr[2].replace("\r", ""));

//            System.out.println("=====>>method:" + this.method);

//            System.out.println("=====>>url:" + this.url);

    }

    public String getRemoveAddr() {
        return removeAddr;
    }

    public void setRemoveAddr(String removeAddr) {
        this.removeAddr = removeAddr;
    }

    public Integer getRemovePort() {
        return removePort;
    }

    public void setRemovePort(Integer removePort) {
        this.removePort = removePort;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
